﻿using System;
using System.Reflection;
using System.Configuration;
using Maticsoft.IDAL;
namespace Maticsoft.DALFactory
{
	/// <summary>
	/// 抽象工厂模式创建DAL。
	/// web.config 需要加入配置：(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口) 
	/// DataCache类在导出代码的文件夹里
	/// <appSettings> 
	/// <add key="DAL" value="Maticsoft.SQLiteDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
	/// </appSettings> 
	/// </summary>
	public sealed class DataAccess//<t>
	{
		private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];
		/// <summary>
		/// 创建对象或从缓存获取
		/// </summary>
		public static object CreateObject(string AssemblyPath,string ClassNamespace)
		{
			object objType = DataCache.GetCache(ClassNamespace);//从缓存读取
			if (objType == null)
			{
				try
				{
					objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
					DataCache.SetCache(ClassNamespace, objType);// 写入缓存
				}
				catch
				{}
			}
			return objType;
		}
		/// <summary>
		/// 创建数据层接口
		/// </summary>
		//public static t Create(string ClassName)
		//{
			//string ClassNamespace = AssemblyPath +"."+ ClassName;
			//object objType = CreateObject(AssemblyPath, ClassNamespace);
			//return (t)objType;
		//}
		/// <summary>
		/// 创建DishInfo数据层接口。
		/// </summary>
		public static Maticsoft.IDAL.IDishInfo CreateDishInfo()
		{

			string ClassNamespace = AssemblyPath +".DishInfo";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (Maticsoft.IDAL.IDishInfo)objType;
		}


		/// <summary>
		/// 创建DishTypeInfo数据层接口。
		/// </summary>
		public static Maticsoft.IDAL.IDishTypeInfo CreateDishTypeInfo()
		{

			string ClassNamespace = AssemblyPath +".DishTypeInfo";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (Maticsoft.IDAL.IDishTypeInfo)objType;
		}

		/// <summary>
		/// 创建HallInfo数据层接口。
		/// </summary>
		public static Maticsoft.IDAL.IHallInfo CreateHallInfo()
		{

			string ClassNamespace = AssemblyPath +".HallInfo";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (Maticsoft.IDAL.IHallInfo)objType;
		}

		/// <summary>
		/// 创建ManagerInfo数据层接口。
		/// </summary>
		public static Maticsoft.IDAL.IManagerInfo CreateManagerInfo()
		{

			string ClassNamespace = AssemblyPath +".ManagerInfo";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (Maticsoft.IDAL.IManagerInfo)objType;
		}

		/// <summary>
		/// 创建MemberInfo数据层接口。
		/// </summary>
		public static Maticsoft.IDAL.IMemberInfo CreateMemberInfo()
		{

			string ClassNamespace = AssemblyPath +".MemberInfo";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (Maticsoft.IDAL.IMemberInfo)objType;
		}

		/// <summary>
		/// 创建MemberTypeInfo数据层接口。
		/// </summary>
		public static Maticsoft.IDAL.IMemberTypeInfo CreateMemberTypeInfo()
		{

			string ClassNamespace = AssemblyPath +".MemberTypeInfo";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (Maticsoft.IDAL.IMemberTypeInfo)objType;
		}

		/// <summary>
		/// 创建OrderDetailInfo数据层接口。
		/// </summary>
		public static Maticsoft.IDAL.IOrderDetailInfo CreateOrderDetailInfo()
		{

			string ClassNamespace = AssemblyPath +".OrderDetailInfo";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (Maticsoft.IDAL.IOrderDetailInfo)objType;
		}

		/// <summary>
		/// 创建OrderInfo数据层接口。
		/// </summary>
		public static Maticsoft.IDAL.IOrderInfo CreateOrderInfo()
		{

			string ClassNamespace = AssemblyPath +".OrderInfo";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (Maticsoft.IDAL.IOrderInfo)objType;
		}

		/// <summary>
		/// 创建TableInfo数据层接口。
		/// </summary>
		public static Maticsoft.IDAL.ITableInfo CreateTableInfo()
		{

			string ClassNamespace = AssemblyPath +".TableInfo";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (Maticsoft.IDAL.ITableInfo)objType;
		}

}
}